package com.tianzj.boot.mvc.service.system.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianzj.boot.mvc.entity.system.SysUserRole;
import com.tianzj.boot.mvc.repository.system.SysUserRoleRepository;
import com.tianzj.boot.mvc.service.system.SysUserRoleService;

@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

	
	@Autowired
	private SysUserRoleRepository  sysUserRoleRepository;
	
	@Override
	public List<Long> getRoleIdByUserId(Long userId) {
		return sysUserRoleRepository.getRoleIdByUserId(userId);
	}

	@Override
	@Transactional
	public void addUserRole(Long userId, String roleIds) {
		if(StringUtils.isNotBlank(roleIds)){
			String[] roleIdArray = roleIds.split(",");
			for (String roleId : roleIdArray) {
				SysUserRole sysUserRole=new SysUserRole();
				sysUserRole.setRoleId(Long.valueOf(roleId));
				sysUserRole.setUserId(userId);
				sysUserRoleRepository.save(sysUserRole);
			}
		}
		
	}

	@Override
	public List<SysUserRole> getUserRoleByUserId(Long userId) {
		return sysUserRoleRepository.findByUserId(userId);
	}

	@Override
	@Transactional
	public void deleteByUserId(Long userId) {
		sysUserRoleRepository.deleteByUserId(userId);
	}

	@Override
	public boolean getUserRoleByRoleIds(String ids) {
		boolean flag=true;
		if(StringUtils.isNotBlank(ids)){
			String[] roleIdArray = ids.split(",");
			for (String roleId : roleIdArray) {
				List<SysUserRole> sysUserRoles =sysUserRoleRepository.findByRoleId(Long.parseLong(roleId));
				if(sysUserRoles!=null&&sysUserRoles.size()!=0){
					flag=false;
					break;
				}
			}
		}
		return flag;
	}
	
	
}
